package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.FreeZe;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentService {

    @Reference
    private CommentApi commentApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    /**
     * 查询评论列表
     * @param publishId  动态id
     * @param page  当前页
     * @param pagesize  页面大小
     * @return
     */
    public ResponseEntity queryCommentsList(String publishId, Integer page, Integer pagesize) {
        //1. 根据动态的id查找评论列表  pageResult ,当前pageResult存储Comment
       PageResult pageResult =  commentApi.queryCommentsList(publishId,page,pagesize);

        //2. 得到pageResult的items
        List<Comment> commentList = (List<Comment>) pageResult.getItems();

        //3. 创建voList
        List<CommentVo> voList = new ArrayList<>();
        //4. 把CommentList转移到volist
        if(commentList!=null){
            for (Comment comment : commentList) {
                CommentVo vo = new CommentVo();
                UserInfo userInfo = userInfoApi.findById(comment.getUserId());
                if(userInfo!=null) {
                    vo.setAvatar(userInfo.getAvatar());
                    vo.setNickname(userInfo.getNickname());
                }
                vo.setId(comment.getId().toString());
                vo.setContent(comment.getContent());
                vo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy年MM月dd日 HH:mm:ss"));
                vo.setLikeCount(comment.getLikeCount());
                vo.setHasLiked(0);
                voList.add(vo);
            }
        }
        //5. pageresult重新设置items，把voList设置到页面对象
        pageResult.setItems(voList);

        //6. 返回响应
        return ResponseEntity.ok(pageResult);

    }


    /**
     * 发表评论
     * @param publishId
     * @param comment
     * @return
     */
    public ResponseEntity saveComments(String publishId, String commentText) {
        //1. 获取登陆者的id
        Long userId = UserHolder.getUserId();

        //        禁止发评论
        String freezekey="FREEZE_KEY_"+userId;
        String freeZEString = redisTemplate.opsForValue().get(freezekey);


        FreeZe freeZe = JSON.parseObject(freeZEString, FreeZe.class);
        if(freeZe!=null) {
            if (freeZe.getFreezingRange() == 2) {

                String reasons = freeZe.getReasonsForFreezing();
                //        秒数
                Long expire = redisTemplate.opsForValue().getOperations().getExpire(freezekey);
//            -1永久冻结 -2没有此键
                String day = null;
                if (expire != -1) {
                    day = "还要冻结" + expire / 60 / 60 / 24 + "天"+expire+"秒";
                } else {
                    day = "永久冻结";
                }
                return ResponseEntity.status(404).body(day + "原因:" + reasons);
            }
        }
        //2. 创建Comment,补全的信息
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(2); //评论的类型， 1. 点赞 2 评论  3 喜欢
        comment.setPubType(1); //评论内容类型： 1. 朋友圈 2 视频  3.评论
        comment.setUserId(userId);
        comment.setContent(commentText);
        comment.setLikeCount(0);//注意：这个是评论的点赞数，不是动态的点赞数据，而且该功能我们没有实现
        comment.setCreated(System.currentTimeMillis());
        commentApi.save(comment);


        return ResponseEntity.ok(null);
    }
}
